package com.ycm.config;

import com.ycm.enums.BaseEnum;
import com.ycm.serializers.CustomEnumDeserializer;
import com.ycm.serializers.CustomEnumSerializer;
import com.ycm.serializers.CustomLocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.TimeZone;

/**
 * Jackson配置
 *
 * @author ycm
 * @date 2024-07-11
 */
@Configuration
public class JacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
                .timeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai")))
                // 配置Jackson在序列化BaseEnum和反序列化Enum时，使用我们自己定义的BaseEnumSerializer、EnumDeserializer进行处理
                .serializerByType(BaseEnum.class, new CustomEnumSerializer())
                .deserializerByType(BaseEnum.class, new CustomEnumDeserializer())
                .serializerByType(LocalDateTime.class, new CustomLocalDateTimeSerializer())
                .build();
    }
}
